package com.ybfenpan.controller.admin.member;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ybfenpan.controller.core.BaseController;
import com.ybfenpan.model.Member;
import com.ybfenpan.model.Pan;
import com.ybfenpan.service.MemberService;
import com.ybfenpan.service.PanService;
import com.ybfenpan.utils.MapUtil;

@Controller
public class PanController extends BaseController {

    @Resource
    private MemberService memberService;
    @Resource
    private PanService panService;


    @RequestMapping("admin/member/pan.shtml")
    public String kuang(HttpServletRequest request, ModelMap model)
            throws Exception {
        Map formMap = getParameterMap(request);
        Member member1 = null;

        Member member21 = null;
        Member member22 = null;

        Member member31 = null;
        Member member32 = null;
        Member member33 = null;
        Member member34 = null;

        //传递的参数
        String loginName = MapUtil.getString(formMap, "loginName");
        if (loginName == null || loginName.length() < 1) {
            member1 = this.memberService.executFirstLevel();
        } else {
            member1 = memberService.executMemberByLoginName(loginName);
            if (member1 == null) {
                member1 = this.memberService.executFirstLevel();
                setMessage(model, "代理商不存在");
            }
        }

        //拿到Panid
        //Pan   7个人 ID
        //传递的参数

        if (member1 != null) {
            Pan pan = this.panService.getByKey(member1.getPanId());
            member1 = this.memberService.getByKey(pan.getPanId());
            if (member1 != null) {
                String loginName1 = member1.getLoginName();
                String phoneNumber1 = loginName1.substring(0, 3) + "****" + loginName1.substring(7, loginName1.length());
                request.setAttribute("phoneNumber1", phoneNumber1);
                // 获得第二层
                member21 = memberService.getByKey(pan.getZuoPanId());

                if (member21 != null) {
                    String loginName21 = member21.getLoginName();
                    String phoneNumber21 = loginName21.substring(0, 3) + "****" + loginName21.substring(7, loginName21.length());
                    request.setAttribute("phoneNumber21", phoneNumber21);
                }
                member22 = memberService.getByKey(pan.getYouPanId());
                if (member22 != null) {
                    String loginName22 = member22.getLoginName();
                    String phoneNumber22 = loginName22.substring(0, 3) + "****" + loginName22.substring(7, loginName22.length());
                    request.setAttribute("phoneNumber22", phoneNumber22);
                }
            }

            // 获得第三层
            if (member21 != null) {

                member31 = memberService.getByKey(pan.getZuoPanZuoId());

                if (member31 != null) {
                    String loginName31 = member31.getLoginName();
                    String phoneNumber31 = loginName31.substring(0, 3) + "****" + loginName31.substring(7, loginName31.length());
                    request.setAttribute("phoneNumber31", phoneNumber31);

                }
                member32 = memberService.getByKey(pan.getZuoPanYouId());
                if (member32 != null) {
                    String loginName32 = member32.getLoginName();
                    String phoneNumber32 = loginName32.substring(0, 3) + "****" + loginName32.substring(7, loginName32.length());
                    request.setAttribute("phoneNumber32", phoneNumber32);

                }

            }
            if (member22 != null) {

                member33 = memberService.getByKey(pan.getYouPanZuoId());
                if (member33 != null) {
                    String loginName33 = member33.getLoginName();
                    String phoneNumber33 = loginName33.substring(0, 3) + "****" + loginName33.substring(7, loginName33.length());
                    request.setAttribute("phoneNumber33", phoneNumber33);

                }

                member34 = memberService.getByKey(pan.getYouPanYouId());
                if (member34 != null) {
                    String loginName34 = member34.getLoginName();
                    String phoneNumber34 = loginName34.substring(0, 3) + "****" + loginName34.substring(7, loginName34.length());
                    request.setAttribute("phoneNumber34", phoneNumber34);

                }

            }

        }

        request.setAttribute("member1", member1);
        request.setAttribute("member21", member21);
        request.setAttribute("member22", member22);
        request.setAttribute("member31", member31);
        request.setAttribute("member32", member32);
        request.setAttribute("member33", member33);
        request.setAttribute("member34", member34);
        return "admin/member/pan";
    }

}
